using System;
using System.ComponentModel;
using System.Runtime.Serialization;
using ExFlash.Records;

namespace ExFlash.Actions
{
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public class ActionPushTypeValuePair : ISwfSerializable
    {
        private readonly int _swfVersion;
        
        public ActionPushVarType Type
        {
            get;
            private set;
        }

        public Object Var
        {
            get;
            private set;
        }

        public ActionPushTypeValuePair(int version)
        {
            _swfVersion = version;
        }

        #region ISwfSerializable Members

        public void SwfDeserialize(BinaryBitReader reader)
        {
            Type = (ActionPushVarType)reader.ReadByte();

            switch (Type)
            {
                case ActionPushVarType.String:
                    {
                        StringRecord str = new StringRecord(_swfVersion);
                        str.SwfDeserialize(reader);
                        Var = str;
                        break;
                    }
                case ActionPushVarType.Float:
                    {
                        Var = reader.ReadSingle();
                        break;
                    }
                case ActionPushVarType.Null:
                    {
                        break;
                    }
                case ActionPushVarType.Undefined:
                    {
                        break;
                    }
                case ActionPushVarType.Register:
                    {
                        Var = reader.ReadByte();
                        break;
                    }
                case ActionPushVarType.Boolean:
                    {
                        Var = reader.ReadByte() > 0;
                        break;
                    }
                case ActionPushVarType.Double:
                    {
                        Var = reader.ReadDouble();
                        break;
                    }
                case ActionPushVarType.Integer:
                    {
                        Var = reader.ReadUInt32();
                        break;
                    }
                case ActionPushVarType.Const8:
                    {
                        Var = reader.ReadByte();
                        break;
                    }
                case ActionPushVarType.Const16:
                    {
                        Var = reader.ReadUInt16();
                        break;
                    }
                default:
                    {
                        throw new SerializationException("Unknown Variable Type in ActionPush");
                    }
            }
        }

        public void SwfSerialize(byte[] arr, ref int offset)
        {
            throw new NotImplementedException();
        }

        #endregion

        public override string ToString()
        {
            return Type + " - " + Var;
        }
    }
}